Ext.Loader.setConfig({
    enabled: true
});

Ext.useShims = true;
Ext.BLANK_IMAGE_URL = '';

Ext.application({
    name: 'MyApp',
    launch: function() {

    	var store = Ext.create('Ext.data.Store', {
            autoLoad: false,
            fields: ['id', 'fileName', 'fileType', 'fileSize', 'filePercent', 'status', 'code', 'picId']
        });


    	var grid = Ext.create('Ext.grid.Panel', {
    		height: 400,
    		width: 750,
    		rowLines: true,
			columnLines: true,
		    columns: [{
		        header: 'Id',
		        dataIndex: 'id',
		        hidden: true,
		        sortable: true,
		        menuDisabled: true
		    }, {
		        xtype: 'rownumberer',
		        width: 30
		    }, {
		        header: '文件名称',
		        flex: 1,
		        dataIndex: 'fileName',
		        sortable: true,
		        menuDisabled: true
		    },/** {
		        header: '文件类型',
		        width: 100,
		        dataIndex: 'fileType',
		        sortable: true,
		        menuDisabled: true
		    }, **/{
		        header: '文件大小',
		        width: 100,
		        dataIndex: 'fileSize',
		        sortable: true,
		        menuDisabled: true
		    }, {
		        header: '详细进度',
		        width: 100,
		        dataIndex: 'filePercent',
		        sortable: true,
		        menuDisabled: true
		    }, {
		        header: '状态',
		        width: 200,
		        dataIndex: 'status',
		        sortable: true,
		        menuDisabled: true
		    }, {
		        xtype: 'actioncolumn',
		        width: 20,
		        items: [{
		            icon: 'images/delete_record.png',
		            tooltip: '移除此记录',
		            handler: function(grid, rowIndex, colIndex, item , eOpts, record) {
		                var id = record.get('id');
		                grid.store.remove(record);
		                //未上传成功的移除掉
		    			if('uploadSuccess' != record.get('code')){
		    				grid.up('grid').uploader.cancelFile(id);
		    			}
		            }
		        }]
		    }],
		    store: store,
		    tbar: [{
	            xtype: 'label',
	            width: 83,
	            height: 24,
	            html: '<div style="line-height:24px;" id="webuploadPciker">' + 
	            	'<img style="position:relative;top:4px;left:-5px;" src="images/add.png" />' + 
	            	'选择文件</div>',
	        }, '-', {
	            iconCls: 'icon-picUpload',
	            text: '开始上传',
	            handler: function(){
	                grid.uploader.upload();
	            }
	        }, '-', {
	            iconCls: 'icon-delete',
	            text: '清空列表',
	            handler: function(){
	        		for(var i = 0; i < store.getCount();i++){
	        			var record = store.getAt(i);
	        			var file_id = record.get('id');
	        			//未上传成功的移除掉
	        			if('uploadSuccess' != record.get('code')){
	        				grid.uploader.cancelFile(file_id);
	        			}
	        		}
	        		store.removeAll();
	            }
	    	}]
    	});
    	
    	grid.on('afterrender', function(){
        	var uploader = grid.uploader = WebUploader.create({
                //swf文件路径
                swf: 'js/webuploader/Uploader.swf',
                //文件接收服务端。
                server: 'pic/save',
                //选择文件的按钮。可选。 内部根据当前运行是创建，可能是input元素，也可能是flash.
                pick: '#webuploadPciker',
                //不压缩image, 默认如果是jpeg，文件上传前会压缩一把再上传！
                resize: false,
                fileNumLimit: 10/*me.file_upload_limit*/,
                //单个文件限制2M大小
                fileSingleSizeLimit: 100 * 1024 * 1024, 
                accept: {
                    title: 'Images',
                    /**extensions: 'gif,jpg,jpeg,bmp,png',
                    mimeTypes: 'image/*' **/
					extensions: 'pdf',
                    mimeTypes: 'application/pdf'
                }
            });
        	
            // 当有文件被添加进队列的时候
            uploader.on('fileQueued', function(file) {
                store.add({
                    id : file.id,
                    fileName : file.name,
                    fileSize : Math.round(file.size*100/(1024*1024))/100+"M",
                    fileType : file.type,
                    status : '待上传',
                    filePercent : '0',
                    code: 'fileQueued'
                });
            });
            
            uploader.on('error', function (code, file) {
//            	var name = file.name;
            	var str = '上传出错!请检查后重新上传!错误代码:' + code;
            	switch(code){
       		 		case 'F_DUPLICATE':
       		 			str = '文件重复上传!';
       		 			break;
       		 		case 'Q_TYPE_DENIED':
       		 			str = '文件类型不允许!';
       		 			break;
       		 		case 'F_EXCEED_SIZE':				
       		 			str = '文件大小超出限制,文件大小不能超过2M!'; 
	       				break; 
       		 		case 'Q_EXCEED_SIZE_LIMIT':
       		 			str = '超出空间文件大小!';
	                	break;
       		 		case 'Q_EXCEED_NUM_LIMIT':
       		 			str = '超过上传数量图片限制!';
            			break;
	       		}
            	Ext.Msg.alert('系统提示', str);
            });
            
            // 文件上传过程中创建进度条实时显示。
            uploader.on('uploadProgress', function(file, percentage ) {
                var record = store.getById(file.id);
                record.set('filePercent', parseInt(percentage * 100) + '%');
                record.set('status', '上传中');
                record.set('code', 'uploadProgress');
                record.commit();
            });

            uploader.on('uploadSuccess', function(file, response) {
            	/*var res = Ext.JSON.decode(response._raw);
                var record = store.getById(file.id);
                if(false == res.success){
                	 record.set('status', '上传失败,大数据服务不在线!');
                	 record.set('code', 'uploadSuccess_bigDataServiceOffLine');
                }else{*/
                	record.set('status', '上传成功');
                	record.set('picId', res.picId);
                	record.set('code', 'uploadSuccess');
                /*}*/
                record.commit();
            });

            uploader.on('uploadError', function(file) {
                var record = store.getById(file.id);
                record.set('filePercent', '0');
                record.set('status', '上传失败,网络异常!');
                record.set('code', 'uploadError');
                record.commit();
            });

            uploader.on('uploadComplete', function(file) {
                var record = store.getById(file.id);
            	if('uploadSuccess' == record.get('code')){
            		
            	}
            });
        });

		grid.render(Ext.getBody());
    }
});